<template>
  <div class="login-page">
    <!-- 其他登录相关的内容 -->
    <el-form
    ref="ruleFormRef"
    style="max-width: 2200px;margin-top: -140px;margin-left: 730px;"
    :model="ruleForm"
    label-width="auto"
    class="demo-ruleForm"
    :size="formSize"
    status-icon
  >
    <el-form-item label="登录名" prop="userName" style="margin-top: 6px;">
      <el-input v-model="ruleForm.userName" style="height: 38px;width: 232px;"/>
    </el-form-item>
    <el-form-item label="密码" prop="password" style="margin-top: 10px;">
      <el-input v-model="ruleForm.password" type="password"  style="height: 38px;width: 232px;"/>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)" style="margin-top: 50px;width: 232px;">
        登录
      </el-button>
    </el-form-item>
  </el-form>
  </div>
  
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import axios from 'axios';
import { useRouter } from 'vue-router';
import {useCounterStore} from '@/stores/counter'
const store = useCounterStore()
const router = useRouter()

interface RuleForm {
  userName: string
  password: string

}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  userName: '',
  password: '',

})

const locationOptions = ['Home', 'Company', 'School']


const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      if(ruleForm.userName==""){
        ElMessage.error("用户名不能为空")
        return
      }
      if(ruleForm.password==""){
        ElMessage.error("密码不能为空")
        return
      }
      let obj={
        userName:ruleForm.userName,
        password:ruleForm.password
      }
      axios({
        url:'https://localhost:44391/api/Management/Login',
        method:'post',
        params:obj
      })
      .then(res=>{
        if(res.data.code==100){
          store.counterAccount.accountId=res.data.data.accountId
          store.counterAccount.accountName=res.data.data.accountName
          store.counterAccount.accountNicName=res.data.data.accountNicName
          store.counterAccount.accountRoleName=res.data.data.roleName
          ElMessage.success(res.data.msg)
        router.push('/product')
        }else{
          ElMessage.error(res.data.msg)
          // setcode()
          return
        }
        console.log(res)
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
// onMounted(()=>{
//   setcode()
// })
</script>

<style>
  .login-page {
    /* background-image: url('../../assets/images/2.png');
    background-size: cover;
    background-position: center;
    height: 100vh; */
    /* 可选的其他背景样式 */
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  height: 100vh;
  width: 1200px;
  background-image: url(../assets/3.png); /* 替换为你的背景图片路径 */
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
  }
</style>

